package com.ebo.httpcall4j.core.log;

import com.alibaba.fastjson2.JSONObject;
import com.ebo.httpcall4j.core.model.HttpCallLog;
import com.ebo.httpcall4j.core.model.HttpCallRequest;
import com.ebo.httpcall4j.core.model.HttpCallResponse;
import com.ebo.httpcall4j.core.repository.HttpCallLogRepository;

/**
 * http调用日志服务
 */
public class HttpCallLogService {

    private final HttpCallLogRepository repository;

    public HttpCallLogService(HttpCallLogRepository repository) {
        this.repository = repository;
    }

    public void log(String beanName, HttpCallRequest request, HttpCallResponse response, boolean isSuccess, Long rerunOfLogId) {
        if (!request.isLogEnabled()) {
            return;
        }
        HttpCallLog log = new HttpCallLog();
        log.setIp(request.getIp());
        log.setUrl(request.getUrl());
        log.setMethod(request.getMethod().name());
        log.setContentType(request.getContentType());
        log.setSystemName(request.getSystemName());
        log.setFunctionName(request.getFunctionName());
        log.setProcessorName(beanName);
        log.setHeaders(JSONObject.toJSONString(request.getHeaders()));

        if (request.getBody()!=null) {
            // 如果已经字符串化了，就不需要再字符串化了
            if (request.getBody() instanceof String) {
                log.setBody((String) request.getBody());
            } else {
                log.setBody(JSONObject.toJSONString(request.getBody()));
            }
        }

        if (request.getExtensions() != null) {
            if (request.getExtensions() instanceof String) {
                log.setExtensions((String) request.getExtensions());
            } else {
                log.setExtensions(JSONObject.toJSONString(request.getExtensions()));
            }
        }

        log.setResponseStatusCode(response.getStatusCode());
        log.setResponseBody(response.getBody());
        log.setStatus(isSuccess ? "SUCCESS" : "FAILED");
        log.setRerunOfLogId(rerunOfLogId);
        Long logId = repository.save(log);
        response.setLogId(logId);
    }

    public void log(String beanName, HttpCallRequest request, HttpCallResponse response, boolean isSuccess) {
        log(beanName,request, response, isSuccess, null);
    }

    public Long logException(String beanName, HttpCallRequest request, Exception e, Long rerunOfLogId) {
        if (!request.isLogEnabled()) {
            return null;
        }
        HttpCallLog log = new HttpCallLog();
        log.setUrl(request.getUrl());
        log.setMethod(request.getMethod().name());
        log.setContentType(request.getContentType());
        log.setSystemName(request.getSystemName());
        log.setFunctionName(request.getFunctionName());
        log.setProcessorName(beanName);
        log.setHeaders(JSONObject.toJSONString(request.getHeaders()));
        log.setBody(JSONObject.toJSONString(request.getBody()));
        log.setResponseStatusCode(null);
        log.setResponseBody(e.getMessage());
        log.setStatus("FAILED");
        log.setRerunOfLogId(rerunOfLogId);
        return repository.save(log);
    }

    public Long logException(String beanName, HttpCallRequest request, Exception e) {
        return logException(beanName,request, e, null);
    }
}